package com.example;

/**
 * Created by IntelliJ IDEA.
 * User: Ranju Phelan
 * Date: 7/13/11
 * Time: 10:42 PM
 * To change this template use File | Settings | File Templates.
 */

import android.util.Log;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;



public class MyXMLHandler extends DefaultHandler {

    Boolean currentElement = false;
    String currentValue = null;
    public static SitesList sitesList = null;

    public static SitesList getSitesList() {
        return sitesList;
    }

    public static void setSitesList(SitesList sitesList) {
        MyXMLHandler.sitesList = sitesList;
    }


    /**
    * This gets called when the xml document is first opened
    *
    * @throws SAXException
    */
    @Override
    public void startDocument() throws SAXException {
        // _data = new Data();
    }

    /**
    * Called when it's finished handling the document
    *
    * @throws SAXException
    */
    @Override
    public void endDocument() throws SAXException {

    }


    /**
    * This gets called at the start of an element. Here we're also setting the booleans to true if it's at that specific tag. (so we
    * know where we are)
    *
    * @param namespaceURI
    * @param localName
    * @param qName
    * @param atts
    * @throws SAXException
    */
    @Override
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        // Log.v("StartElement", localName);
        currentElement = true;

        if (localName.equals("quiz_capitals")){
            /** Start */
            sitesList = new SitesList();
        }


    }

    /**
    * Called at the end of the element. Setting the booleans to false, so we know that we've just left that tag.
    *
    * @param namespaceURI
    * @param localName
    * @param qName
    * @throws SAXException
    */
    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
        Log.v("endElement", localName);

        currentElement = false;
        // Log.v("Value", currentValue);
        if(localName.equals("question_text")) {
            sitesList.setQuestionText(currentValue);
        }
        else if(localName.equals("option1")) {
            sitesList.setQuestion_option1(currentValue);
        }
        else if(localName.equals("option2")) {
            sitesList.setQuestion_option2(currentValue);
        }
        else if(localName.equals("option3")) {
            sitesList.setQuestion_option3(currentValue);
        }
        else if(localName.equals("option4")) {
            sitesList.setQuestion_option4(currentValue);
        }
        else if(localName.equals("answer")) {
            sitesList.setQuestionAnswer(currentValue);
        }
        else if(localName.equals("actual")) {
            sitesList.setQuestionActual(currentValue);
        }
    }

    /**
    * Calling when we're within an element. Here we're checking to see if there is any content in the tags that we're interested in
    * and populating it in the Config object.
    *
    * @param ch
    * @param start
    * @param length
    */
    @Override
    public void characters(char ch[], int start, int length) {

        if (currentElement) {
            currentValue = new String(ch, start, length);
            currentElement = false;
        }



    }


}